package com.example.administrator.namelist;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

public class StuAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<Student> studentArrayList;
    private IOnDelListener listener;

    public StuAdapter(Context context,ArrayList<Student> studentArrayList,IOnDelListener listener){
        this.context = context;
        this.studentArrayList = studentArrayList;
        this.listener = listener;
    }

    //判断是哪个数组
    public void changeData(ArrayList<Student> studentArrayList){
        this.studentArrayList = studentArrayList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return studentArrayList.size();
    }

    @Override
    public Object getItem(int i) {
        return studentArrayList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    private int count;
    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        /*inflate()
        * 第一个参数：要显示在view上的布局
        * 第二个参数：父布局
        * 第三个参数：父布局可显示view的页面，但不需要构建，因此是false
        * */
        //创建view需要先构建布局构建器

        Log.e("StuAdapter", "getView" + i + "  count = "+ count );
        //优化一：实现view的复用，节省内存
        if(view == null){
            count++;
            LayoutInflater layoutInflater = LayoutInflater.from(context);
            view = layoutInflater.inflate(R.layout.student_list_item,
                    viewGroup,false);

            //优化二：使用ViewHolder减少findView的次数，提高效率
            StuViewHolder holder = new StuViewHolder();
            holder.numView = view.findViewById(R.id.numView);
            holder.imgView = view.findViewById(R.id.imgView);
            holder.nameView = view.findViewById(R.id.nameView);
            holder.ageView = view.findViewById(R.id.ageeView);
            holder.delStu = view.findViewById(R.id.btn_del);

            //将holder封装到view中
            view.setTag(holder);
        }


        //得到holder中的保存数据
        final StuViewHolder holder = (StuViewHolder) view.getTag();

        final Student s = studentArrayList.get(i);
        holder.numView.setText(s.getNumber());
        holder.imgView.setImageResource(s.getImgId());
        holder.nameView.setText(s.getName());
        //由于s.getAge()是int类型，数值在Android中找到的是其资源ID
        //因此要转为String类型，让其显示年龄
        holder.ageView.setText(String.valueOf(s.getAge()));

        //删除
        holder.delStu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //删除操作要让数据的持有者实现
                //可通过监听者
                listener.del(s.getNumber());
            }
        });

        return view;
    }
}
